//
//  AIPersonalMaritalView.m
//  Mia
//
//  Created by Vill on 2024/1/17.
//

#import "AIPersonalMaritalView.h"

@interface AIPersonalMaritalView ()

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, assign) BOOL ischoose;
@property (nonatomic, copy) NSArray *buttonArray;

@end

@implementation AIPersonalMaritalView

-(NSArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSArray arrayWithObjects:@"Single",@"Married",@"Divorced",@"Widowed",@"Separated",nil];
    }
    return _buttonArray;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if(self == [super initWithFrame:frame]){
        [self setUI];
    }
    return self;
}

- (void)setUI{
    CGFloat bthHeight =  PTW(54);
    for (int i = 0; i < self.buttonArray.count ; i++) {
        CGFloat btnX = (KScreenWidth - PTW(30) - PTW(103)*3)/2;
        CGFloat btnY = PTW(69)*(i/3);
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((btnX + PTW(103))*(i%3) , btnY, PTW(103), bthHeight)];
        btn.tag = i + 6;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(reasonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor colorHex:@"#FFFFFF" alpha:1];
        [btn setTitle:self.buttonArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorHex:@"#333333"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont fontWithWeight:Regular size:PTW(15)];
        btn.layer.cornerRadius = PTW(27);
        btn.layer.borderWidth = PTW(1);
        btn.layer.borderColor = UIColor.clearColor.CGColor;
        if(btn.tag == 6){
            btn.selected = YES;
            btn.layer.borderColor = [UIColor colorHex:@"#333333"].CGColor;
        }
    }
}

-(void)reasonClick:(UIButton *)btn{
    self.ischoose = YES;
    if (btn == self.selectBtn) {
        return;
    }
    for (int i = 0; i < self.buttonArray.count; ++i) {
        if (btn.tag == 6 + i) {
            btn.selected = YES;
            self.selectBtn = btn;
            btn.layer.borderColor = [UIColor colorHex:@"#333333"].CGColor;
            continue;
        }
        UIButton *but = (UIButton *)[self viewWithTag:6+i];
        but.layer.borderColor = UIColor.clearColor.CGColor;
        but.selected = NO;
    }
}

@end
